#include "mycoin.h"
#include <QTimer>
#include <QDebug>
#include <QPushButton>
#include <QKeyEvent>
MyCoin::MyCoin(QString btnImg)
{

    //设置基础信息
    QPixmap pix;
    bool ret = pix.load(btnImg );
    if(!ret){
        QString str = QString ("图片%1加载失败").arg(btnImg);
        qDebug()<<str;
        return;
    }
    //设置固定大小
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px}");
    //按钮是开辟在堆区,本身只是个按钮,直接设置Icon
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
    //初始化定时器
 timer1 = new QTimer();
 timer2 = new QTimer();
 connect(timer1,&QTimer::timeout,[=](){
     QPixmap pix;
     QString str = QString(":/res/Coin000%1.png").arg(this->min++);
     pix.load(str);
     this->setStyleSheet("QPushButton{border:0px}");
         this->setIcon(pix);
     this->setIconSize(QSize(pix.width(),pix.height()));
//     qDebug()<<"正面翻"<<QString::number(this->min);
     this->setFixedSize(pix.width(),pix.height());
     //如果翻完了,将min重置为1
     if(this->min>this->max){
         this->min=1;
         //定时器停止  动画结束
         this->isAnimation=false;
         timer1->stop();
     }
 });

 connect(timer2,&QTimer::timeout,[=](){
     QPixmap pix;
     QString str = QString(":/res/Coin000%1.png").arg(this->max--);
     pix.load(str);
     this->setStyleSheet("QPushButton{border:0px}");
     this->setIcon(pix);
     this->setIconSize(QSize(pix.width(),pix.height()));
//     qDebug()<<"反面翻"<<QString::number(this->max);
     this->setFixedSize(pix.width(),pix.height());
     //如果翻完了,将min重置为1
     if(this->max < this->min){
         this->max=8;
         //定时器停止  动画结束
         this->isAnimation=false;
         timer2->stop();
     }
 });
}

void MyCoin::changeFlag()
{
    if(this->flag){
        //开启正面翻反面的定时器
        timer1->start(35);
        this->isAnimation=true;
        this->flag=false;
        //加了一个修改状态,另一个状态在playscene中,点击所有金币状态只有一个win状态修复
        this->isWin = false;

    }//反面翻正面
    else
    {
       timer2->start(35);
       this->isAnimation=true;
       this->flag=true;

    }

}



//防止动画中断
void MyCoin::mousePressEvent(QMouseEvent *e)
{

    if(this->isAnimation|| this->isWin ||this->checkPoint){
        return;
    }
    else{
        //让父类执行
        QPushButton::mousePressEvent(e);
    }

}



